<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE html>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="from"%>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/easyui/themes/icon.css">
    <script type="text/javascript" src="<%=request.getContextPath() %>/resources/easyui/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/resources/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/style/main.css">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/style/icon.css">
</head>
<body>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath()%>/resources/js/ajaxfileupload.js"></script>
<script type="text/javascript">
    var uploadUrl={
        uploadMatrix:'<%=request.getContextPath() %>/upload/uploadMatrix',
        readMatrixJson:'<%=request.getContextPath() %>/upload/readMatrixJson',
        createMatrixJson:'<%=request.getContextPath() %>/upload/createMatrixJson',
        uploadCase:'<%=request.getContextPath() %>/upload/uploadCase',
        uploadCases:'<%=request.getContextPath() %>/upload/uploadCases',
        readCase:'<%=request.getContextPath() %>/upload/readCase',
        readCases:'<%=request.getContextPath() %>/upload/readCases',
        downloadCase:'<%=request.getContextPath() %>/upload/downloadCase',
        downloadCases:'<%=request.getContextPath() %>/upload/downloadCases',
        startCaseDownLoad:'<%=request.getContextPath() %>/upload/startCaseDownLoad',
        clearZipAndDir:'<%=request.getContextPath() %>/upload/clearZipAndDir',
        startCasesDownLoad:'<%=request.getContextPath() %>/upload/startCasesDownLoad'
    };
    var caseJson = <%=request.getAttribute("caseJson")%>;
    var catalogId = <%=request.getAttribute("catalogId")%>;
    var case_data_table = document.getElementById("caseTable");
    case_data_table.setAttribute("style","height:"+(document.body.scrollHeight-118)+"px");
    $(function(){
        $('#caseTable').datagrid({
            data:caseJson,
            onClickRow: function(rowIndex,rowData){
                var id = rowData['id'];
                $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/caseData-design/'+id);
                $('body').layout('panel', 'center').panel('setTitle', rowData['name']);
            }
        });
    });
    function doSearch(value){
        if(value==null || value==""){
            $('#caseTable').datagrid({
                data:caseJson,
                onClickRow: function(rowIndex,rowData){
                    var id = rowData['id'];
                    $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/caseData-design/'+id);
                    $('body').layout('panel', 'center').panel('setTitle', rowData['name']);
                }
            });
            return;
        }
        var rows = $('#caseTable').datagrid('getRows');
        var newJson = [];
        var index = 0;
        for(var i=rows.length-1;i>=0;i--){
            if(caseJson[i]['name'].indexOf(value)>=0 || value.indexOf(caseJson[i]['name'])>=0){
                newJson[index++] = caseJson[i];
            }
        }
        $('#caseTable').datagrid('loadData', newJson);
    }
    function importData(){
        $('#caseData_import').click();
    }
    function caseDataChange(){
        var ids = [];
        ids[0] = 'caseData_import';
        var filename = $('#caseData_import').val();
        filename = filename.substring(filename.lastIndexOf("\\")+1);
        $.ajaxFileUpload({
            url:uploadUrl.uploadCases,
            secureuri:false,
            fileElementId:ids,
            dataType:'json',
            success:function(data,status){
                if(data){
                    var progressValue = [];
                    progressValue[0] = 0;
                    $.ajax({
                        url:uploadUrl.readCases,
                        data:'filename='+filename+'&parentId='+catalogId,
                        type:'get',
                        onSubmit:showProgress(true, "提示", "服务器处理中...", progressValue),
                        success:function(result){
                            if(result.successful){
                                if(progressBar!=null){
                                    clearInterval(intervalId);
                                    progressValue[0] = progressBar.progressbar('getValue');
                                    intervalId = setInterval(changeProgress(progressValue,progressBar,5),500);
                                }
                                setTimeout(function(){
                                    //clearInterval(intervalId);
                                    progressBar.progressbar('setValue', 100);
                                    $.messager.progress('close');
                                    clearInterval(intervalId);
                                },5000);
                                $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/showCatalogCases/'+catalogId);
                                var appendTreeJson = eval('('+result.message+')');
                                var selected = $('#help_tree').tree('getSelected');
                                $('#help_tree').tree('append',{
                                    parent:selected.target,
                                    data:appendTreeJson
                                });
                            }else{
                                $.messager.show({title:'操作结果',msg:result.message});
                            }
                        }
                    });
                }
                else{
                    $.messager.alert("提示",data.result,"error");
                }
            },
            error:function(data,status,e){

            }
        });
    }
    function showProgress(isShow, title, msg, value){
        if(!isShow){
            $.messager.progress('close');
            return;
        }
        $.messager.progress({
            title:title,
            msg:msg,
            interval:0
        });
        progressBar = $.messager.progress('bar');
        intervalId = setInterval(changeProgress(value,progressBar,5),500);
    }
    function changeProgress(value,progressBar,step){
        return function() {
            if (value[0] < 100) {
                value[0] += Math.floor(Math.random()*step);
                progressBar.progressbar('setValue', value[0]);
            }else{
                value[0] = 0;
                progressBar.progressbar('setValue', value[0]);
            }
        }
    }
    function exportData(){
        var progressValue = [];
        progressValue[0] = 0;
        alert("catalogId"+catalogId);
        $.ajax({
            url:uploadUrl.downloadCases,
            data:'parentId='+catalogId,
            type:'get',
            onSubmit:showProgress(true, "提示", "服务器处理中...", progressValue),
            success:function(result){
                if(result.successful){
                    if(progressBar!=null){
                        progressValue[0] = progressBar.progressbar('getValue');
                        alert("progressValue[0]"+progressValue[0]);
                        intervalId = setInterval(changeProgress(progressValue,progressBar,5),500);
                        alert("intervalId"+intervalId);
                    }
                    setTimeout(function(){
                        //clearInterval(intervalId);
                        progressBar.progressbar('setValue', 100);
                    },5000);
                    setTimeout(function(){
                        $.messager.progress('close');
                        window.open(uploadUrl.startCasesDownLoad+"?caseId="+catalogId);
                    },10000);
                }
            }
        });
    }
    function rowformater(value,row,index){
        var id = row['id'];
        var name = row['name'];
        return "<a href='#' onclick='clicka(" + id + "," + name + ")'>" + name + "</a>";
    }
    function clicka(id,name){
        $('#show_win').panel('refresh','<%=request.getContextPath() %>/page/caseData-design/'+id);
        $('body').layout('panel', 'center').panel('setTitle', name);
    }
</script>
<div id="toolbar">
    <input  id="searchBox" class="easyui-searchbox" data-options="prompt:'查找...',searcher:doSearch" style="width:300px" />
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-undo" plain="true" onclick="importData()">导入</a>
    <input id="caseData_import" type="file" name="file" style="display:none" onchange="caseDataChange()"/>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-redo" plain="true" onclick="exportData()">导出CC</a>
</div>
<table id="caseTable" class="easyui-datagrid"
       data-options="singleSelect:true,collapsible:true,toolbar:'#toolbar'" rownumbers="true" noheader="true">
    <thead>
    <tr>
        <th data-options="field:'name',resizable:false,align:'center',width:200,formatter:rowformater"><strong>实例名称</strong></th>
        <th data-options="field:'time',resizable:false,align:'center',width:200"><strong>修改时间</strong></th>
        <th data-options="field:'reviser',resizable:false,align:'center',width:200"><strong>修改人</strong></th>
    </tr>
    </thead>
</table>
</body>
</html>
